package org.smardi.fakeCall;

import java.util.*;

import org.smardi.fakeCall.CLIQr.*;
import org.smardi.fakeCall.Service.*;
import org.smardi.fakeCall.Setting.*;
import org.smardi.fakeCall.Timer.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class AC_FakeMain extends Activity {

	ComponentName fakeService = null;
	
	LinearLayout btn_timer = null, btn_cliqr = null, btn_setting = null;
	
	TextView btn_exit = null;
	
	long time_firstExitTry = 0;
	
	SharedPreference_FakeCall mPref = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_fakemain);

		Intent intent = new Intent(AC_FakeMain.this, SV_FakeCall.class);

		bindService(intent, srvConnection, Context.BIND_AUTO_CREATE);
		
		initComponent();
		
		mPref = new SharedPreference_FakeCall(AC_FakeMain.this);
	}

	private void initComponent() {
		btn_exit = (TextView)findViewById(R.id.btn_top_left);
		
		btn_timer = (LinearLayout)findViewById(R.id.btn_timer);
		btn_cliqr = (LinearLayout)findViewById(R.id.btn_cliqr);
		btn_setting = (LinearLayout)findViewById(R.id.btn_setting);
		
		registEvent();
	}

	private void registEvent() {
		btn_exit.setOnClickListener(viewOnClick);
		
		btn_timer.setOnClickListener(viewOnClick);
		btn_cliqr.setOnClickListener(viewOnClick);
		btn_setting.setOnClickListener(viewOnClick);
	}
	
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		// 서비스는 여러번 불려도 한번만 종료하면 됨
		unbindService(srvConnection);
		stopService(new Intent(AC_FakeMain.this, fakeService.getClass()));
	}

	@Override
	protected void onResume() {
		super.onResume();

		// 서비스 시작
		fakeService = startService(new Intent(AC_FakeMain.this,
				SV_FakeCall.class));
		
		loadPreference();
	}

	private void loadPreference() {
		ImageView img_tail_timer = (ImageView)findViewById(R.id.img_tail_timer);
		if(0 < mPref.getTargetTime()) {
			img_tail_timer.setImageDrawable(getResources().getDrawable(R.drawable.main_light_on));
		} else {
			img_tail_timer.setImageDrawable(getResources().getDrawable(R.drawable.main_light_off));
		}
		
		ImageView img_tail_cliqr = (ImageView)findViewById(R.id.img_tail_cliqr);
		if(mPref.isCLIQrMode() == true) {
			img_tail_cliqr.setImageDrawable(getResources().getDrawable(R.drawable.main_light_on));
		} else {
			img_tail_cliqr.setImageDrawable(getResources().getDrawable(R.drawable.main_light_off));
		}
	}

	@Override
	protected void onPause() {
		super.onPause();

		//액티비티는 멈춰도 서비스는 살아있어야 하므로
		//서비스를 종료하는 부분은 onDestroy에 넣는다.
	}
	//==========================================================
	View.OnClickListener viewOnClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_top_left:
				exit();
				break;
			
			case R.id.btn_timer:
				startActivity(new Intent(AC_FakeMain.this, AC_Timer.class));
				break;
			case R.id.btn_cliqr:
				startActivity(new Intent(AC_FakeMain.this, AC_CLIQr.class));
				break;
			case R.id.btn_setting:
				startActivity(new Intent(AC_FakeMain.this, AC_Setting.class));
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	

	// =========================================================

	
	private SV_FakeCall serviceBinder;

	private ServiceConnection srvConnection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			serviceBinder = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			serviceBinder = ((SV_FakeCall.bsBinder) service).getService();
		}
	};

	protected void exit() {
		long now = new Date().getTime();
		
		if(now - time_firstExitTry > 1000) {
			//나가려면 다시 한번 눌러달라고 한다
			Toast.makeText(AC_FakeMain.this, "나가려면 다시 한번 눌러 주세요.", 1000).show();
			time_firstExitTry = now;
		} else {
			finish();
		}
	}
}
